package com.czl.module_storehouse.mvp.model;

import android.util.Log;

import com.czl.base.BuildConfig;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.base.data.net.RetrofitClient;
import com.czl.base.util.SpHelper;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_service.system.Constants;
import com.czl.module_storehouse.api.ApiService;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @author by xubin,
 * @date on 2022/2/15.
 * PS: Not easy to write code, please indicate.
 */
public class SortModel extends BasicDataModel {
    public Observable<HttpResponse<SortBean>> getSortCode(String sortPid, String pCode) {
        JSONObject object = new JSONObject();
        try {
            object.put("sortPId", sortPid);
            object.put("sortPCode", pCode);
            object.put("companyId", SpHelper.INSTANCE.decodeInt(Constants.SpKey.MANAGE_COMPANY_ID));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json = object.toString();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class)
                .getSortCode(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<SortBean>> addSortInfo(SortBean bean) {
        bean.setCompanyId(SpHelper.INSTANCE.decodeInt(Constants.SpKey.MANAGE_COMPANY_ID));
        bean.setCompanyName(SpHelper.INSTANCE.decodeString(Constants.SpKey.MANAGE_COMPANY_NAME));
        bean.setCompanyShortName(SpHelper.INSTANCE.decodeString(Constants.SpKey.MANAGE_COMPANY_SHORT_NAME));
        Gson gson = new Gson();
        String json = gson.toJson(bean);
        if (BuildConfig.DEBUG) {
            Log.e(TAG, "SortBean :%s" + json);
        }
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class)
                .addSortInfo(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<SortBean>> editSortInfo(SortBean bean) {
        bean.setCompanyId(SpHelper.INSTANCE.decodeInt(Constants.SpKey.MANAGE_COMPANY_ID));
        bean.setCompanyName(SpHelper.INSTANCE.decodeString(Constants.SpKey.MANAGE_COMPANY_NAME));
        bean.setCompanyShortName(SpHelper.INSTANCE.decodeString(Constants.SpKey.MANAGE_COMPANY_SHORT_NAME));
        Gson gson = new Gson();
        String json = gson.toJson(bean);
        if (BuildConfig.DEBUG) {
            Log.e(TAG, "SortBean :%s" + json);
        }
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class)
                .editSortInfo(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<ListBean<SortBean>>> getSortList(int pageNo, Boolean pasteCode, List<Integer> ids, Integer priceInfo, String name) {
        JSONObject object = new JSONObject();
        try {
            if (pasteCode != null) {
                object.put("isPasteCode", pasteCode);
            }
            if (ids != null && !ids.isEmpty()) {
                JSONArray array = new JSONArray();
                for (Integer i : ids) {
                    array.put(i);
                }
                object.put("sortPIdList", array);
            }
            object.put("pageNo", pageNo);
            if (priceInfo != null && priceInfo > 0) {
                object.put("priceInfo", priceInfo);
                object.put("companyId", getCompanyId());
            } else {
                object.put("companyId", getManageCompanyId());
            }
            if (name != null && !name.isEmpty()) {
                object.put("sortName", name);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json = object.toString();
        return RetrofitClient.getInstance().create(ApiService.class)
                .getSortList(createBody(json));
    }

    public Observable<HttpResponse<Object>> deleteSortInfo(int sortId) {
        JSONObject object = new JSONObject();
        try {
            object.put("sortId", sortId);
            object.put("storehouseId", getStorehouseId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RetrofitClient.getInstance().create(ApiService.class)
                .deleteSortInfo(createBody(object));
    }

    public Observable<HttpResponse<ListBean<SortBean>>> getSortList(String sortName, int pageNo) {
        JSONObject object = new JSONObject();
        try {
            object.put("companyId", getManageCompanyId());
            object.put("sortName", sortName);
            object.put("stockInfo", 1);
            object.put("storehouseId", getStorehouseId());
            object.put("pageNo", pageNo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RetrofitClient.getInstance().create(ApiService.class)
                .getSortList(createBody(object));
    }
}
